package com.tj.collection.utils;

import com.fqgj.common.utils.TimeUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 日期转换
 *
 * @author liuming
 * Created on 下午5:36 2018/9/28.
 */
public class DateUtils {

    private static final long ONE_DAY_TIME_MILLIS = 1000 * 60 * 60 * 24;

    public static String formatEnglishDate(Date date) {
        String format = new SimpleDateFormat("MMM d", Locale.ENGLISH).format(date);
        String dateEnd = new SimpleDateFormat("d", Locale.ENGLISH).format(date);
        if (dateEnd.equals("1")) {
            dateEnd = "st";
        } else if (dateEnd.equals("2")) {
            dateEnd = "nd";
        } else if (dateEnd.equals("3")) {
            dateEnd = "rd";
        } else {
            dateEnd = "th";
        }
        format = format + dateEnd;
        return format;
    }

    public static String getDatePoor(Date startDate, Date endDate) {
        if (endDate == null) {
            endDate = new Date();
        }
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - startDate.getTime();
        //// 计算差多少天
        //long day = diff / nd;
        // 计算差多少小时
        long hour = diff / nh;
        // 计算差多少分钟
        long min = diff % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return hour + "H" + min + "m";
    }

    public static int getAgeByBirth(Date birthday) {
        // 当前时间
        Calendar curr = Calendar.getInstance();
        // 生日
        Calendar born = Calendar.getInstance();
        born.setTime(birthday);
        // 年龄 = 当前年 - 出生年
        int age = curr.get(Calendar.YEAR) - born.get(Calendar.YEAR);
        if (age <= 0) {
            return 0;
        }
        // 如果当前月份小于出生月份: age-1
        // 如果当前月份等于出生月份, 且当前日小于出生日: age-1
        int currMonth = curr.get(Calendar.MONTH);
        int currDay = curr.get(Calendar.DAY_OF_MONTH);
        int bornMonth = born.get(Calendar.MONTH);
        int bornDay = born.get(Calendar.DAY_OF_MONTH);
        if ((currMonth < bornMonth) || (currMonth == bornMonth && currDay <= bornDay)) {
            age--;
        }
        return age < 0 ? 0 : age;
    }

    public static int overDueDays(Date repaymentDate, Date payOffDate) {
        repaymentDate = TimeUtils.getDate("yyyy-MM-dd", repaymentDate);
        Date nowDate = TimeUtils.getDate("yyyy-MM-dd", payOffDate != null ? payOffDate : new Date());
        Integer overdueDays = TimeUtils.diffDays(repaymentDate, nowDate);
        if (overdueDays < 0) {
            overdueDays = 0;
        }
        return overdueDays;
    }

    public static double getDaysDiff(Date start, Date end) {
        long startTime = start.getTime();
        long endTime = end.getTime();
        return (endTime-startTime)/ONE_DAY_TIME_MILLIS;
    }

    public static int getDays(long millis) {
        return Math.round(millis/ONE_DAY_TIME_MILLIS);
    }

}
